/**
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.active;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.BaseFragment;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.mode.Address;
import com.tstudy.laoshibang.utils.LogUtil;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
@EFragment(R.layout.active_map)
public class ActiveMapFragment extends BaseFragment{
	final static String TAG = "active_map";
	public ActiveMapFragment() {
		super();
	}
	
	@ViewById(R.id.active_map_root)
	RelativeLayout mRootLayout;
	
//	@ViewById(R.id.active_map_view)
	MapView mMapView;
	
	boolean mIsFirstLoad = true;
	Handler mHandler;
	LayoutInflater mInflater;
	
	private BaiduMap mBaiduMap;
	Address mAddress;
	double mLat;
	double mLon;
	BitmapDescriptor icon;

	public static void add(int containerViewId,double lat,double lon) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
		args.putDouble(CONSTANT.ARGS.LAT,lat);
		args.putDouble(CONSTANT.ARGS.LON, lon);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,ActiveMapFragment_.class.getName(),args);
		((BaseActivity) BaseApplication.mCurrentActivity).add(containerViewId, fragment, TAG);
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mHandler = new Handler();
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		icon = BitmapDescriptorFactory
				.fromResource(R.drawable.icon_gcoding);
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@AfterViews
	public void initView(){
		if (mIsFirstLoad) {
			LatLng center = new LatLng(mLat, mLon);
			MapStatus mapStatus = new MapStatus.Builder().target(center).build();
			mMapView = new MapView(getActivity(),new BaiduMapOptions().mapStatus(mapStatus));
			RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
			lp.addRule(RelativeLayout.BELOW,R.id.active_map_title_layout);
			mRootLayout.addView(mMapView,lp);
			mRootLayout.setOnClickListener(null);
			mBaiduMap = mMapView.getMap();
			MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);
			mBaiduMap.setMapStatus(msu);
			initOverlay();
			mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
				public boolean onMarkerClick(final Marker marker) {
					return true;
				}
			});
			
		}
	}
	@Click({R.id.active_map_back})
	public void actionClick(View view){
		switch (view.getId()) {
		case R.id.active_map_back:
			backAction(mFragmentId);
			break;
		
		default:
			break;
		}
		
	}
	public void initOverlay() {
		LatLng lat = new LatLng(mLat,mLon);
		OverlayOptions overlay = new MarkerOptions().position(lat).icon(icon)
				.zIndex(9).draggable(true);
		mBaiduMap.addOverlay(overlay);
	}

	/**
	 * 清除所有Overlay
	 * 
	 * @param view
	 */
	public void clearOverlay(View view) {
		mBaiduMap.clear();
	}

	/**
	 * 重新添加Overlay
	 * 
	 * @param view
	 */
	public void resetOverlay(View view) {
		clearOverlay(null);
		initOverlay();
	}

	@Override
	public void onPause() {
		// MapView的生命周期与Activity同步，当activity挂起时需调用MapView.onPause()
		mMapView.onPause();
		super.onPause();
	}

	@Override
	public void onResume() {
		// MapView的生命周期与Activity同步，当activity恢复时需调用MapView.onResume()
		mMapView.onResume();
		super.onResume();
	}

	@Override
	public void onDestroy() {
		// MapView的生命周期与Activity同步，当activity销毁时需调用MapView.destroy()
		mMapView.onDestroy();
		super.onDestroy();
		// 回收 bitmap 资源
		icon.recycle();
		
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
			mLat = args.getDouble(CONSTANT.ARGS.LAT);
			mLon = args.getDouble(CONSTANT.ARGS.LON);
		}
	}
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

}
